package ch.jhc.sevlet;

import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;

import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import ch.jhc.Global;



/**
 * Servlet implementation class VcodeServlet
 */
@WebServlet("/VcodeServlet")
public class VcodeServlet extends HttpServlet implements Global {
	private static final long serialVersionUID = 1L;
	private static final int IMAGE_WIDTH = 120;
	private static final int IMAGE_HEIGHT = 36;
	private static final Random RANDOM = new Random();
    private static final String CHARS = "0123456789abcdefghijklmnopqrstuvwxyzQWERTYUIOPLKJHGFDSAZXCVBNM";

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		BufferedImage bImage = new BufferedImage(IMAGE_WIDTH, IMAGE_HEIGHT, BufferedImage.TYPE_INT_BGR);
		Graphics2D graphics2d = bImage.createGraphics();
		graphics2d.setFont(new Font("Arial", Font.BOLD, 24));
		String code = rand();
		System.out.println("VcodeServlet: " + code);
		request.getSession().setAttribute(VCODE_KEY, code);
		graphics2d.drawString(code, 20, 30);
		response.setContentType("image/png");
		ImageIO.write(bImage, "png",response.getOutputStream());
	}
	private String rand() {
		String result = "";
		for(int i=0;i<4;i++) {
			int index = RANDOM.nextInt(CHARS.length());
			result += CHARS.substring(index, index + 1);
		}
		return result;
	}

	/*HttpSession httpSession = null;
	private StringBuffer stringBuffer= new StringBuffer();
	String x = null;
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		char [] zifu={'a','b','c','d','e','f','g','h','i','j','k','l','m','n',
				'o','p','q','r','s','t','u','v','w','x','y','z','A','B','C','D','E',
				'F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V',
				'W','X','Y','Z','1','2','3','4','5','6','7','8','9','0'};
		Random random = new Random();
		httpSession = request.getSession();
		response.setContentType("image/png");
		BufferedImage bImage = new BufferedImage(160, 70, BufferedImage.TYPE_INT_BGR);
		Graphics2D graphics2d = bImage.createGraphics();
		for(int i=1;i<5;i++) {
				x=""+zifu[random.nextInt(35)];
				graphics2d.setFont(new Font(null,Font.BOLD, 25+random.nextInt(10)));
				graphics2d.setColor(Color.getHSBColor(random.nextInt(255), random.nextInt(255), random.nextInt(255)));
				graphics2d.rotate(random.nextInt(25)*0.002);
				graphics2d.drawString(x,15+(i*15), 10+(i*13));
				stringBuffer.append(x);
		}
		httpSession.setAttribute("VCode", stringBuffer.toString());
		System.out.println(stringBuffer);
		stringBuffer.setLength(0);
		ImageIO.write(bImage, "png",response.getOutputStream());
	
		用户的code请求  放进去
		request.getSession().getAttribute(VCODE_KEY,code);	
		}*/
}